if (typeof Vue == "undefined")
	document.write(
		'<script src="/scriptZIP/vue.min.js"></script>'
	);

if (typeof axios == "undefined")
	document.write(
		'<script src="/scriptZIP/axios.min.js"></script>'
	);

(function () {
	var body = document.body.innerHTML;
	if (body.indexOf("qs.min.js") >= 0 && typeof Qs == "undefined")
		document.write(
			'<script src="/scriptZIP/qs.min.js"></script>'
		);
	if (body.indexOf("vue-select.min.js") >= 0 && typeof VueSelect == "undefined")
		document.write(
			'<script src="/scriptZIP/vue-select.min.js"></script>'
		);
})();

onerror = function (msg, url, line) {
	function shorten(s) {
		if (s.startsWith(location.origin)) s = s.slice(location.origin.length);
		return s;
	}
	if (typeof axios == "undefined" || !url) return;
	axios
		.post("/api/report", {
			href: shorten(location.href),
			msg: msg,
			url: shorten(url),
			line: line,
		})
		.then(function (res) { });
};

function params() {
	
	var q = location.search; //查找id
	if (!q) return {};
	q = q.slice(1);
	return Qs.parse(q);
}

function top_params() {
	var q;
	try {
		// 可能会报错的代码
		q = top.location.search;
	} catch (error) {
		// 错误处理代码
		console.log(error); // 输出错误信息
	}
	// var q = top.location.search;
	if (!q) return {};
	q = q.slice(1);
	q = Qs.parse(q);
	if (!q.id) q.id = localStorage.schoolId;
	return q;
}

function copy(o) {
	if (Array.isArray(o)) return o.concat();
	return Object.assign({}, o);
}

function compare(o1, o) {
	var a = {};
	for (var k in o) if (o1[k] != o[k]) a[k] = o1[k];
	a.id = o.id;
	return a;
}

function toFormParams(o) {
	var params = new URLSearchParams();
	for (var key in o) {
		var value = o[key];
		if (value !== undefined && value !== null) params.append(key, value);
	}
	return params;
}

function toSet(array) {
	var o = {};
	array.forEach(function (it) {
		o[it] = true;
	});
	return o;
}

if (typeof axios != "undefined") {
    let sessionExpired = false; // 标志位，防止多次弹窗

    axios.interceptors.response.use(
        function (res) {
            var type = res.headers["content-type"];
            
            if (type && type.indexOf("text/html") >= 0) {
                if (!sessionExpired) {
                    sessionExpired = true; // 设置标志位，避免重复弹窗
                    
                    if (location.pathname.startsWith("/wechat/")) {
                        history.back();
                        close();
                    } else {
                        alert("会话已过期，请重新登录");
                        top.location.href = "/";
                    }
                }
                return; // 阻止对这个响应的进一步处理
            }
            
            return res;
        },
		function (error) {
			var status = error.response.status;
			if (status == 400 || status == 402) {
				alert(error.response.data.msg);
			} else if (status == 401) {
				history.back();
				close();
			} else if (status == 403) {
				alert("权限不足");
			} else if (status == 404) {
				alert("url错了");
			} else if (status == 413) {
				alert("上传文件过大");
			} else if (status == 502) {
				alert("系统正在更新，请稍等片刻");
			} else if (status == 504) {
				alert("抱歉，系统忙碌，查询超时。请勿频繁重复操作");
			} else alert("抱歉，出错了");
			return Promise.reject(error);
		}
	);
}

function getStudent() {
	var s = sessionStorage["hxt-student"];
	return s ? JSON.parse(s) : null;
}

function getTeacher() {
	var s = sessionStorage["hxt-teacher"];
	return s ? JSON.parse(s) : null;
}

function isTeacher() {
	var s = sessionStorage["hxt-user"];
	return s == "teacher";
}

function getPerson() {
	return isTeacher() ? getTeacher() : getStudent();
}

function getUnreadKey() {
	return "hxt-unread-" + getPerson().id;
}

function getUnread() {
	var s = localStorage[getUnreadKey()];
	return s ? JSON.parse(s) : {};
}

function setUnread(unread) {
	localStorage[getUnreadKey()] = JSON.stringify(unread);
}

function updateUnread(field) {
	var unread = getUnread();
	unread[field] = new Date().getTime();
	setUnread(unread);

	var cache = getUnreadCache();
	if (cache) {
		cache[field] = 0;
		setUnreadCache0(cache);
	}
}

function setUnreadCache0(unread) {
	sessionStorage[getUnreadKey()] = JSON.stringify(unread);
}

function setUnreadCache(unread) {
	unread.time = new Date().getTime();
	setUnreadCache0(unread);
}

function getUnreadCache() {
	var key = getUnreadKey();
	var s = sessionStorage[key];
	if (!s) return null;
	s = JSON.parse(s);
	if (new Date().getTime() - s.time > 3e5) {
		sessionStorage.removeItem(key);
		return null;
	}
	return s;
}

// if (/^https:/.test(location.href))
// 	setInterval(function () {
// 		eval(atob('ZGVidWdnZXI='));
// 	}, 999);
